const db = require('../db/index')
// const bcryptjs = require('bcryptjs')
// const jwt = require('jsonwebtoken')
// const config = require('../config')

exports.getList = (req, res) => {
    const { page, limit } = req.body
    db.getConnection((err, connection) => {
        if (err) {
            return res.send({ status: 1, message: err.message })
        }
        let sqlStr = 'select * from song limit ?,?'
        // let sqlStr = 'select *,(select * from user where id=(select userId from song))as user from song limit ?,?'
        let params = [(parseInt(page) - 1) * parseInt(limit), parseInt(limit)]
        connection.query(sqlStr, params, (err, results) => {
            results.forEach(item => {
                sqlStr = 'select * from user where id=?'
                connection.query(sqlStr, item.userId, (err, results) => {
                    item["user"] = results[0]
                    return item
                })
            })
            setTimeout(() => { res.send(results) }, 100)
        })
    })
}

exports.getInfo = (req, res) => {
    const { id } = req.params
    db.getConnection((err, connection) => {
        if (err) {
            return res.send({ status: 1, message: err.message })
        }
        let sqlStr = 'select * from song where id=?'
        connection.query(sqlStr, id, (err, results) => {
            sqlStr = 'select * from user where id=?'
            connection.query(sqlStr, results[0].userId, (err, result) => {
                results[0]["user"] = result[0]
                res.send(results)
            })
        })
    })
}
